﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MysqlCoreDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            string connectionString = "server=localhost;" +
                                    "port=3306;" +
                                    "username=root;" +
                                    "password=cn1985218;" +
                                    "database=new_schema;";
            MySqlConnection connection = new MySqlConnection(connectionString);
            connection.Open();
            //连接到数据库后，可以执行SQL查询，如下所示
            MySqlCommand command = new MySqlCommand("SELECT * FROM new_table;", connection);
            MySqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader["column1"] + " " + reader["column2"]);
            }
            //关闭连接
            connection.Close();

            //Database.CreateIfNotExists();

            EFmysqlContext context = new EFmysqlContext();
            //context.Database.CreateIfNotExists(); /*如果数据库不存在，创建新的数据库*/
            //Database.SetInitializer(new CreateDatabaseIfNotExists<EFmysqlContext>());/*如果数据库不存在，创建新的数据库*/
            //Database.SetInitializer(new DropCreateDatabaseAlways<EFmysqlContext>());/*始终创建新的数据库*/
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EFmysqlContext>());/*如果模型改变，创建新的数据库*/
            context.Users.Add(new User { Name=DateTime.Now.ToString() ,age=DateTime.Now.Second});
            var result = context.SaveChanges();
            Console.WriteLine(result);
        }
    }
}
